﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Peekaboo2008.dao;
using Peekaboo2008.common;

namespace Peekaboo2008.view
{
    public partial class FormTimKiemPhieuHang : Form
    {
        private DataTable dbDanhSachPhieuhang;
        private String Opts = "";
        private String IgnoreDateTime = "";
        private String From = "";
        private String To = "";
        public FormTimKiemPhieuHang()
        {
            InitializeComponent();
            initLoaiHoaDon();
        }

        private void initLoaiHoaDon(){
            cbbLoaiHoaDon.Items.Add("Tất cả");
            cbbLoaiHoaDon.Items.Add("Xuất");
            cbbLoaiHoaDon.Items.Add("Nhập");
            cbbLoaiHoaDon.SelectedIndex = 0;
        }

        private void btnTimKiem_Click(object sender, EventArgs e)
        {
            Opts = cbbLoaiHoaDon.SelectedIndex.ToString();
            IgnoreDateTime = ckbTatCa.Checked ? "1" : "0";
            From = dtpTuNgay.Value.ToShortDateString();
            To = dtpDenNgay.Value.ToShortDateString();
            TimKiem(Opts,IgnoreDateTime,From,To);
            grvDanhSachPhieuHang.ClearSelection();
        }

        private void ckbTatCa_CheckedChanged(object sender, EventArgs e)
        {
            if(ckbTatCa.Checked){
                dtpDenNgay.Enabled = false;
                dtpTuNgay.Enabled = false;
            }else{
                dtpDenNgay.Enabled = true;
                dtpTuNgay.Enabled = true;
            }
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        private void grvDanhSachPhieuHang_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex > -1 & e.RowIndex < grvDanhSachPhieuHang.Rows.Count)
            {
                String MaPhieuNhapXuat = grvDanhSachPhieuHang.Rows[e.RowIndex].Cells["clmID"].Value.ToString();
                FormPhieuHangChiTiet _FormPhieuHangChiTiet = new FormPhieuHangChiTiet(MaPhieuNhapXuat);
                _FormPhieuHangChiTiet.ShowDialog();
            }

        }

        private void TimKiem(String _Opts, String _IgnoreDateTime,String _From,String _To)
        {

            dbDanhSachPhieuhang = PhieuNhapXuatDAO.GetByFromTo(Constants.USER_LOGIN.MaChiNhanh , _Opts, _IgnoreDateTime, _From, _To);
            if (dbDanhSachPhieuhang == null || dbDanhSachPhieuhang.Rows.Count == 0 )
            {
                lblThongBao.Text = "*Danh sách không tồn tại";
            }
            else
            {
                grvDanhSachPhieuHang.DataSource = dbDanhSachPhieuhang;
                lblThongBao.Text = "";
            }
        }

        private void FormTimKiemPhieuHang_KeyDown(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.F6:
                    Opts = cbbLoaiHoaDon.SelectedIndex.ToString();
                    IgnoreDateTime = ckbTatCa.Checked ? "1" : "0";
                    From = dtpTuNgay.Value.ToShortDateString();
                    To = dtpDenNgay.Value.ToShortDateString();
                    TimKiem(Opts,IgnoreDateTime,From,To);
                break;
                case Keys.F7:
                    this.Dispose();
                break;
            }
        }

        private void grvDanhSachPhieuHang_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            //if (e.RowIndex > -1 & e.RowIndex < grvDanhSachPhieuHang.Rows.Count)
            //{
            //    String _InStatus = grvDanhSachPhieuHang.Rows[e.RowIndex].Cells["clmXuatHoaDon"].Value.ToString();
            //    if (_InStatus.Equals(""))
            //    {
            //        btnIn.Enabled = true;
            //    }
            //    else
            //    {
            //        btnIn.Enabled = false;
            //    }
            //}
        }

        //private void btnIn_Click(object sender, EventArgs e)
        //{
        //    String _ID = grvDanhSachPhieuHang.Rows[grvDanhSachPhieuHang.CurrentRow.Index].Cells["clmID"].Value.ToString();
        //    PhieuNhapXuatDAO.updateInHoaDon(_ID);
        //    TimKiem(Opts,IgnoreDateTime,From,To);
        //    grvDanhSachPhieuHang.ClearSelection();
        //    btnIn.Enabled = false;
        //}
    }
}
